import { ContactsMgr, contacts_mgr } from "@/mgr/contact/ContactsMgr";
import { IContactItem } from "@/mgr/contact/IContactItem";
import { useEffect, useState } from "react";

export function useWXContacts() {
  const [contacts, set_contacts] = useState<Readonly<IContactItem[]>>([])
  const [mgr, set_mgr] = useState<ContactsMgr>(contacts_mgr);
  useEffect(() => {
    const contacts_mgr = new ContactsMgr();
    contacts_mgr.load();
    set_mgr(contacts_mgr);
    set_contacts(contacts_mgr.list ?? []);
    return contacts_mgr.callbacks.add({
      on_list_change: () => set_contacts(contacts_mgr.list ?? [])
    });
  }, [ContactsMgr]);
  return { contacts, mgr }
}

